package cn.edu.sdu.order.backend.service.impl;

import cn.edu.sdu.order.backend.dao.FpgaMapper;
import cn.edu.sdu.order.backend.dao.OrderMapper;
import cn.edu.sdu.order.backend.dto.FpgaDTO;
import cn.edu.sdu.order.backend.model.Fpga;
import cn.edu.sdu.order.backend.model.FpgaExample;
import cn.edu.sdu.order.backend.model.Order;
import cn.edu.sdu.order.backend.model.OrderExample;
import cn.edu.sdu.order.backend.service.FpgaService;
import cn.edu.sdu.order.backend.service.KeyMaxValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FpgaServiceImpl implements FpgaService {

    @Autowired
    private FpgaMapper fpgaMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private KeyMaxValueService keyMaxValueService;

    @Override
    public String update(FpgaDTO fpgaDTO) {
        FpgaExample fpgaExample = new FpgaExample();
        fpgaExample.createCriteria()
                .andMacEqualTo(fpgaDTO.getMac());
        List<Fpga> fpgaList = fpgaMapper.selectByExample(fpgaExample);
        String fpgaCode = fpgaList.get(0).getFpgaCode();
        OrderExample orderExample = new OrderExample();
        orderExample.createCriteria()
                .andFpgaCodeEqualTo(fpgaCode);
        List<Order> orders = orderMapper.selectByExample(orderExample);
        if(orders.isEmpty()) {
            throw new RuntimeException("更新错误");
        } else {
            Order order = orders.get(0);
            order.setTrack(fpgaDTO.getLocation().get(0).toString()+","+fpgaDTO.getLocation().get(1).toString());
            order.setTemperature(fpgaDTO.getTemperature());
            order.setHumidity(fpgaDTO.getHumidity());
        }
        return "done";
    }
}
